<template lang="pug">
  .clearfix
    Modal(
      width="480"
      v-model="isShowTaxPayModal"
      title="确认个税付款"
      class-name="vertical-center-modal"
    )
      .search-form
        Form.ivu-row(ref="formData" :model="formData" :rules="rules" :label-width="100")
          FormItem.form-item.ivu-col.pr-20(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable :disabled="item.disabled")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.bankName"
                  :key="index"
                ) {{ c_item.bankName }}
            template(v-if="item.type === 'datePicker'")
              DatePicker.w-100(v-model="formData[item.model]" placeholder="请选择" type="date" format="yyyy-MM-dd" @on-change="handleChangeDatePicker(item.model, $event)")

      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="confirmPay" :loading="!isCanSubmit") 确定
</template>

<script>
import { SalaryPayableBillApi } from '@/api/SalaryPayableBill.api.js'
import { OrganizationBankApi } from '@/api/OrganizationBank.api.js'
import { Common } from '@/utils/common.js'
export default {
  name: '',

  props: {
  },

  components: {
  },

  data () {
    return {
      organizationName: '',
      isShowTaxPayModal: false,
      formData: {
        amount: '',
        payDate: new Date(),
        bankName: '',
        periodYear: '',
        periodMonth: ''
      },
      formItem: [
        { type: 'input', label: '付款金额', placeholder: '请输入付款金额', prop: 'amount', model: 'amount', style: 'ivu-col-span-24', disabled: true },
        { type: 'datePicker', label: '付款时间', placeholder: '请选择付款时间', prop: 'payDate', model: 'payDate', style: 'ivu-col-span-24' },
        { type: 'select', label: '付款银行', placeholder: '请选择付款银行', prop: 'bankName', model: 'bankName', style: 'ivu-col-span-24', options: 'bankList' }
      ],
      rules: {
        amount: [
          { required: true, message: '请输入付款金额' }
        ],
        payDate: [
          { required: true, message: '请选择付款时间' }
        ],
        bankName: [
          { required: true, message: '请选择付款银行' }
        ],
      },
      arrListDown: {
        bankList: []
      },
      isCanSubmit: true,
      defaultParamsData: {
        feeDateStart: null,
        feeDateEnd: null,
        companyName: null
      }
    }
  },

  watch: {
  },

  mounted () {
  },

  methods: {
    cancel () {
      this.isShowTaxPayModal = false
    },

    show () {
      this.$refs.formData.resetFields()
      this.isCanSubmit = true
      this.isShowTaxPayModal = true
    },

    // 时间选择控件
    handleChangeDatePicker (model, value) {
      this.formData[model] = value
      var timeArr = value.split('-')
      this.formData.periodYear = timeArr[0]
      this.formData.periodMonth = timeArr[1]
    },

    // // 获取银行账号下拉列表
    // async getBankList() {
    //   var params = {
    //     organizationName: this.organizationName,
    //     HasPaging: true,
    //     PageIndex: 1,
    //     PageSize: 100
    //   }
    //   let res = await OrganizationBankApi.getList(params)
    //   console.log("获取银行账号下拉列表res: ", res)
    //   if (res.status === 200 && res.data.success) {
    //     this.arrListDown.bankList = res.data.data
    //   } else {
    //   }
    //   this.loading = false
    // },

    // 获取银行账号下拉列表
    async getBankList() {
      var params = {
        id: JSON.parse(localStorage.getItem('clientLoginInfo')).data.companyId
      }
      let res = await OrganizationBankApi.getBankList(params)
      console.log("获取银行账号下拉列表res: ", res)
      if (res.status === 200 && res.data.success) {
        this.arrListDown.bankList = res.data.data
      } else {
      }
      this.loading = false
    },

    // 校验并获取当前个税总额
    async checkConfirmPay () {
      SalaryPayableBillApi.checkToConfirmPay(this.defaultParamsData).then(res => {
        console.log("校验并获取当前个税总额res: ", res)
        if (res.data.data && res.data.success) {
          this.isShowTaxPayModal = true
          this.formData.amount = res.data.data
        } else {
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },

    // 个税付款确认
    async confirmPay () {
      var params = {
        Amount: this.formData.amount,
        PayDate: this.formData.payDate,
        BankName: this.formData.bankName
      }
      console.log('params: ', params)
      this.$refs.formData.validate((valid) => {
        if (valid) {        
          SalaryPayableBillApi.toConfirmPay({...this.defaultParamsData,...params}).then(res => {
            console.log("个税付款确认res: ", res)
            if (res.status === 200 && res.data.success) {
              this.$Message.success(res.data.message)
              this.$emit('query')
              this.isShowTaxPayModal = false
            } else {
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scope>
</style>
